package io.itit.ecp.admin.provide.filter;

import com.alibaba.fastjson.serializer.ValueFilter;
import io.itit.grass.common.server.helper.DictHelper;
import io.itit.grass.common.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * <p>
 *
 * @Author：Roy
 * @Description：设备管理键值转换
 * @Date：Create by 2023-11-30 18:51:39
 */
public interface IDeviceFilter {

	String DEVICE_TYPE = "deviceType" ;
	String PRODUCT_TYPE = "productType" ;
	String DEVICE_STATUS = "deviceStatus" ;

	String DEVICE_DEVICE_TYPE = "ecp-admin.device.deviceType" ;
	String DEVICE_PRODUCT_TYPE = "ecp-admin.device.productType" ;
	String DEVICE_DEVICE_STATUS = "ecp-admin.device.deviceStatus" ;

	class DeviceExportFilter implements ValueFilter {

		private Map<String, String> deviceTypeMap;
		private Map<String, String> productTypeMap;
		private Map<String, String> deviceStatusMap;

		public DeviceExportFilter() {
			deviceTypeMap = DictHelper.i().stringVlMap(DEVICE_DEVICE_TYPE);
			productTypeMap = DictHelper.i().stringVlMap(DEVICE_PRODUCT_TYPE);
			deviceStatusMap = DictHelper.i().stringVlMap(DEVICE_DEVICE_STATUS);
		}

		@Override
		public Object process(Object object, String name, Object value) {
			if (StringUtils.equals(name, DEVICE_TYPE)) {
				return CommonUtils.orElse(deviceTypeMap.get(CommonUtils.toString(value)), value);
			}
			if (StringUtils.equals(name, PRODUCT_TYPE)) {
				return CommonUtils.orElse(productTypeMap.get(CommonUtils.toString(value)), value);
			}
			if (StringUtils.equals(name, DEVICE_STATUS)) {
				return CommonUtils.orElse(deviceStatusMap.get(CommonUtils.toString(value)), value);
			}
			return value;
		}

	}

	class DeviceUploadFilter implements ValueFilter {

		private Map<String, String> deviceTypeMap;
		private Map<String, String> productTypeMap;
		private Map<String, String> deviceStatusMap;

		public DeviceUploadFilter() {
			deviceTypeMap = DictHelper.i().stringLvMap(DEVICE_DEVICE_TYPE);
			productTypeMap = DictHelper.i().stringLvMap(DEVICE_PRODUCT_TYPE);
			deviceStatusMap = DictHelper.i().stringLvMap(DEVICE_DEVICE_STATUS);
		}

		@Override
		public Object process(Object object, String name, Object value) {
			if (StringUtils.equals(name, DEVICE_TYPE)) {
				return CommonUtils.orElse(deviceTypeMap.get(CommonUtils.toString(value)), value);
			}
			if (StringUtils.equals(name, PRODUCT_TYPE)) {
				return CommonUtils.orElse(productTypeMap.get(CommonUtils.toString(value)), value);
			}
			if (StringUtils.equals(name, DEVICE_STATUS)) {
				return CommonUtils.orElse(deviceStatusMap.get(CommonUtils.toString(value)), value);
			}
			return value;
		}

	}

}